<?php

class AdminRssEditController extends AdminIndexController
{
    public function prepare()
    {
        $id = Env::getParam('id');

        if ($id) {
            $rm = new RssModel();
            $rss = $rm->get($id);
            
            if (!$rss) {
                throw new HTTP404Exception();
            }
            $this->view->assign('rss', $rss);
        }
        
        ApplicationController::attachEventListener($this, array($_POST, 'RssSaveAction'));
    }
    
    public function exec()
    {
        $response = array(
            'error' => 1,
            'text'  => 'При попытке сохранения произошла ошибка'
        );
        
        $id = Env::post('id');
        $url = Env::post('url');
        $status = Env::post('status');
        
        $rss_model = new RssModel(DbConnection::COMMON_SPACE, true);
        if ($rss_model->edit(new Rss($id, $url, $status))) {
            $response['error'] = 0;
            $response['text']  = '';
        }
        
        echo json_encode($response);
        die();
    }
}